package com.magic.goods.listener;

import com.magic.goods.service.GoodsHtmlService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author lan.jianyuan
 * @create 2021-01-29-10:07
 */
@Component
public class GoodsListener {
    
    @Autowired
    private GoodsHtmlService goodsHtmlService;
    
    /**
     * 处理insert和update的消息
     *
     * @param id
     * @throws Exception
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "magic.create.web.queue", durable = "true"),
            exchange = @Exchange(
                    value = "magic.item.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC),
            key = {"item.insert", "item.update", "item.unShelves"}))
    public void listenCreate(Long id) {
        if (id == null) {
            return;
        }
        // 创建或更新索引
        this.goodsHtmlService.createHtml(id);
    }
    /**
     * 处理delete的消息
     *
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "magic.delete.web.queue", durable = "true"),
            exchange = @Exchange(
                    value = "magic.item.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC),
            key = "item.delete"))
    public void listenDelete(Long id) {
        if (id == null) {
            return;
        }
        // 删除索引
        this.goodsHtmlService.deleteHtml(id);
    }
}
